package com.ocom.human.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.human.EntityResourceApi;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.StringUtils;
import com.ocom.human.dao.IDaoResourceApi;
import com.ocom.human.service.ResourceApiService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.bouncycastle.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ResourceApiServiceImpl extends ServiceImpl<IDaoResourceApi, EntityResourceApi> implements ResourceApiService {


    @Autowired
    private IDaoResourceApi iDaoResourceApi;

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityResourceApi> selectList(Integer parentId) {
        QueryWrapper<EntityResourceApi> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("status",1);
        if (ObjectUtil.isNotEmpty(parentId)) {
            if (parentId == 1) {
                queryWrapper.gt("parent_id", 0);
            } else if (parentId == -1) {
                queryWrapper.eq("parent_id", -1);
            } else {
                queryWrapper.eq("parent_id", parentId);
            }
        }
        queryWrapper.orderByAsc("api_name");
        return this.list(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public Integer WebPage(String pageCode, String pageName,Integer parentId) {
        QueryWrapper<EntityResourceApi> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        queryWrapper.eq("api_code",pageCode);
        queryWrapper.last(" limit 1");
        EntityResourceApi pageCodeResourceApi=iDaoResourceApi.selectOne(queryWrapper);
        if (pageCodeResourceApi!=null){
            return 1;
        }

        QueryWrapper<EntityResourceApi> queryWrapperName=new QueryWrapper<>();
        queryWrapperName.eq("parent_id",parentId);
        queryWrapperName.eq("api_name",pageCode);
        queryWrapperName.last(" limit 1");
        EntityResourceApi pageNameResourceApi=iDaoResourceApi.selectOne(queryWrapperName);
        if (pageNameResourceApi !=null){
            return 2;
        }
        return 0;
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public Boolean saveResourceApi(EntityResourceApi entityResourceApi) {
        return this.save(entityResourceApi);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityResourceApi> selectWebButtonList(Integer pageId) {
        QueryWrapper<EntityResourceApi> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",pageId);
        queryWrapper.eq("status",1);
        queryWrapper.orderByAsc("api_name");
        return iDaoResourceApi.selectList(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public Result modifyWebPage(Integer pageId, String pageName, String description,String pageCode,String pageAppCode,Integer parentId) {
        QueryWrapper<EntityResourceApi> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        queryWrapper.eq("id",pageId);
        queryWrapper.last(" limit 1");
        EntityResourceApi entityResourceApi=iDaoResourceApi.selectOne(queryWrapper);
        if (entityResourceApi ==null){
            return ResultUtil.error(ResultEnum.Err_4046);
        }

        QueryWrapper<EntityResourceApi> pageCodeQueryWrapper=new QueryWrapper<>();
        pageCodeQueryWrapper.eq("parent_id",parentId);
        pageCodeQueryWrapper.ne("id",pageId);
        pageCodeQueryWrapper.eq("api_code",pageCode);
        pageCodeQueryWrapper.last(" limit 1");
        EntityResourceApi  pageCodeEntityResourceApi=iDaoResourceApi.selectOne(pageCodeQueryWrapper);
        if (pageCodeEntityResourceApi !=null){
            return ResultUtil.error(ResultEnum.Err_4044);
        }

        QueryWrapper<EntityResourceApi> queryWrappers=new QueryWrapper<>();
        queryWrappers.ne("id",pageId);
        queryWrappers.eq("parent_id",parentId);
        queryWrappers.eq("api_name",pageName);
        queryWrappers.last(" limit 1");
        EntityResourceApi entityResourceApis=iDaoResourceApi.selectOne(queryWrappers);
        if (entityResourceApis !=null){
            return ResultUtil.error(ResultEnum.Err_4045);
        }
        EntityResourceApi updateEntityResourceApi=new EntityResourceApi();
        updateEntityResourceApi.setId(pageId);
        updateEntityResourceApi.setApiName(pageName);
        updateEntityResourceApi.setApiCode(pageCode);
        updateEntityResourceApi.setDescription(description);
        updateEntityResourceApi.setPageAppCode(pageAppCode);
        if (this.updateById(updateEntityResourceApi)){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.Err_3026);
        }
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityResourceApi selectWebButton(Integer buttonId) {
        QueryWrapper<EntityResourceApi> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",buttonId);
        return iDaoResourceApi.selectOne(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public Boolean deleteWebButton(Integer buttonId) {
        return this.removeById(buttonId);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public Result getRoleButtonPermission(Integer pageId, String buttonCode, String buttonName, String description) {
        QueryWrapper<EntityResourceApi> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",pageId);
        queryWrapper.eq("api_code",buttonCode);
        queryWrapper.last(" limit 1");
        EntityResourceApi pageCodeResourceApi=iDaoResourceApi.selectOne(queryWrapper);
        if (pageCodeResourceApi!=null){
            return ResultUtil.error(ResultEnum.Err_4048);
        }

        QueryWrapper<EntityResourceApi> queryWrapperName=new QueryWrapper<>();
        queryWrapperName.eq("parent_id",pageId);
        queryWrapperName.eq("api_name",buttonName);
        queryWrapperName.last(" limit 1");
        EntityResourceApi pageNameResourceApi=iDaoResourceApi.selectOne(queryWrapperName);
        if (pageNameResourceApi !=null){
            return ResultUtil.error(ResultEnum.Err_4049);
        }

        EntityResourceApi entityResourceApi=new EntityResourceApi();
        entityResourceApi.setParentId(pageId);
        entityResourceApi.setApiCode(buttonCode);
        entityResourceApi.setApiName(buttonName);
        entityResourceApi.setDescription(description);
        Date date=new Date();
        entityResourceApi.setCreateTime(date);
        entityResourceApi.setUpdateTime(date);
        entityResourceApi.setStatus((byte) 1);
        if (this.save(entityResourceApi)){
            Map<String,Object> map=new HashMap<>();
            map.put("buttonId",entityResourceApi.getId());
            return ResultUtil.success(map);
        }else {
            return ResultUtil.error(ResultEnum.Err_2016);
        }
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public Result modifyWebButton(Integer pageId, Integer buttonId, String buttonName, String description) {
        QueryWrapper<EntityResourceApi> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",pageId);
        queryWrapper.eq("id",buttonId);
        queryWrapper.last(" limit 1");
        EntityResourceApi entityResourceApi=iDaoResourceApi.selectOne(queryWrapper);
        if (entityResourceApi ==null){
            return ResultUtil.error(ResultEnum.Err_4050);
        }

        QueryWrapper<EntityResourceApi> queryWrappers=new QueryWrapper<>();
        queryWrappers.ne("id",buttonId);
        queryWrappers.eq("parent_id",pageId);
        queryWrappers.eq("api_name",buttonName);
        queryWrappers.last(" limit 1");
        EntityResourceApi entityResourceApis=iDaoResourceApi.selectOne(queryWrappers);
        if (entityResourceApis !=null){
            return ResultUtil.error(ResultEnum.Err_4051);
        }
        EntityResourceApi updateEntityResourceApi=new EntityResourceApi();
        updateEntityResourceApi.setId(buttonId);
        updateEntityResourceApi.setApiName(buttonName);
        updateEntityResourceApi.setDescription(description);
        if (this.updateById(updateEntityResourceApi)){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.Err_3026);
        }
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityResourceApi> getPageList(Integer role,Integer weOrApp) {
        if(ObjectUtil.isEmpty(weOrApp)){
            weOrApp = 0;
        }
        return iDaoResourceApi.getPageList(role,weOrApp);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityResourceApi> getButtoneList(Integer role) {
        return iDaoResourceApi.getButtoneList(role);
    }
}
